package com.wut.clock.controller;

import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.entity.Favorite;
import com.wut.clock.mongo.dto.CollectionParams;
import com.wut.clock.service.ICollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 收藏管理
 * @date 2022/6/11 11:28
 */
@RequiredArgsConstructor
@RequestMapping("/collection")
@RestController
@Api(value="收藏管理器",tags = {"收藏管理器"})
public class CollectionController {

    private final ICollectionService collectionService;

    @ApiOperation("新增收藏夹")
    @PostMapping("addFavorite")
    public CommonResult<String> addFavorite(@RequestBody Favorite favorite){
        if(collectionService.addFavorite(favorite) > 0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("修改收藏夹")
    @PostMapping("updateFavorite")
    public CommonResult<String> updateFavorite(@RequestBody Favorite favorite){
        if(collectionService.updateFavorite(favorite) > 0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("删除收藏夹")
    @PostMapping("deleteFavorite/{favoriteId}")
    public CommonResult<String> deleteFavorite(@PathVariable Integer favoriteId){
        if(collectionService.deleteFavorite(favoriteId) > 0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("获取收藏夹列表")
    @PostMapping("getFavoriteList")
    public CommonResult getFavoriteList(){
        List<Favorite> favoriteList = collectionService.getFavoriteList();
        if(favoriteList != null) return CommonResult.success(favoriteList);
        return CommonResult.failed("当前没有收藏夹");
    }


    @ApiOperation("新增收藏")
    @PostMapping("addCollection")
    public CommonResult<String> addCollection(@RequestBody CollectionParams collectionParams){
        if(collectionService.addCollection(collectionParams) > 0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("删除收藏")
    @PostMapping("deleteCollection/{articleId}")
    public CommonResult<String> deleteCollection(@PathVariable Integer articleId){
        if(collectionService.deleteCollection(articleId) > 0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("获取用户的收藏夹下的收藏")
    @GetMapping("getCollectionList")
    public CommonResult<CommonPage<CollectionParams>> getCollectionList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                                                        @RequestParam Integer favoriteId){
        return CommonResult.success(CommonPage.restPage(collectionService.getCollectionList(pageNum,pageSize,favoriteId)));
    }
}
